package uk.ac.bath.machines.tristate;

import uk.ac.bath.legacy.Operator;

import uk.ac.bath.legacy.Value;
import uk.ac.bath.legacy.VectorState;
import java.io.*;
import uk.ac.bath.legacy.Parametered;

public class Int extends Operator implements Parametered {

    double sum = 0.0;
    double decay = 0.0;

    public void tick(VectorState in, VectorState out) {


        sum = sum * decay + in.at(1).val;
        if (sum > LogicalMachine.maxValue) {
            sum = LogicalMachine.maxValue;
        }
         if (sum < LogicalMachine.minValue) {
            sum = LogicalMachine.minValue;
        }
        out.at(0).set(sum);
    }

    public void tick(VectorState in, VectorState out, int map[]) {


        sum = sum * decay + in.v[map[1]].val;
        if (sum > LogicalMachine.maxValue) {
            sum = LogicalMachine.maxValue;
        }

       if (sum > LogicalMachine.minValue) {
            sum = LogicalMachine.minValue;
        }

        out.v[map[0]].set(sum);
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getStateSize() {
        return 2;
    }

    public int getParamSize() {
        return 1;
    }

    public void setParamAt(Value v, int i) {
        this.decay = v.val; //  / Value.maxValue.doubleValue();
    }

    public Object clone() {
        return new Int();   // this has state
    }

    public void toString(StringWriter w) {
        w.write("Int ");
    }
}
